﻿
namespace TestFsWeb

open System
open Newtonsoft.Json
open Newtonsoft.Json.Linq

module TryJson =

    type Record = {
        Name : string
        Rank : int
    }

    let private serialize_key_values() =
        let array  =
            [("stasi",9);("cheka",66)]
            |> Seq.map (fun (name,value) ->
                                        let obj = new JObject()
                                        obj.Add <| new JProperty("name",name)
                                        obj.Add <| new JProperty("value",value)
                                        obj)
            |> Seq.fold (fun (array : JArray) o -> array.Add o; array ) (new JArray())
        
        // pass formatting as the argument
        // otherwise, the default ToString will use "dented" format
        printfn "%s" <| array.ToString(Formatting.None)

    // it will only serialize the "Array" itself
    // it won't serialize any prooperties of the list itself
    // such as Size or Capacity
    let private test_serialize_list() =
        let records = new ResizeArray<Record>(3)
        records.Add <| {Name = "stasi";Rank = 99}
        records.Add <| {Name = "cheka";Rank = 88}

        records
        |> JsonConvert.SerializeObject
        |> printfn "%s"

    let main() =
        // serialize_key_values()
        test_serialize_list()


    


